#include "StdAfx.h"
#include "DEntityStructure.h"
#include "dentityfactory.h"

DEntityStructure::DEntityStructure(void)
: m_pClassView(0)
{
}

DEntityStructure::~DEntityStructure(void)
{
}

DEntityStructure& DEntityStructure::instance()
{
	static DEntityStructure inst;
	return inst;
}

void DEntityStructure::init(QTreeWidget* pClassView)
{
	m_pClassView = pClassView;
}

DClassEntity* DEntityStructure::baseClass()
{
	QVariant baseClassVar = m_pClassView->topLevelItem(0)->data(0, Qt::UserRole);
	return qVariantValue<DClassEntity*>(baseClassVar);
}

DClassEntity* DEntityStructure::classByItem(QTreeWidgetItem* pItem)
{
	QVariant classVar = pItem->data(0, Qt::UserRole);
	return qVariantValue<DClassEntity*>(classVar);
}

QTreeWidgetItem* DEntityStructure::itemByClass(const DClassEntity* pClass)
{
	// NOTE: this will not work correctly if we have several classes with same name
	QTreeWidgetItemIterator it(m_pClassView);

	while(*it)
	{
		QVariant value = (*it)->data(0, Qt::UserRole);
		DClassEntity* pCheck = qVariantValue<DClassEntity*>(value);
		if(pCheck == pClass)
		{
			return (*it);
		}
		++it;
	}

	return 0;
}